﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Engineer : MonoBehaviour
{

    public bool bStructOrderFollow;  //건설명령 듣는지


    public bool bStructInProgress;  // 건설중?
    public bool bMoveToStruct; // 건설하러 이동
    public GameObject DestStructure; // 목표 건설물
    void Start()
    {
        bStructOrderFollow = false;
        bStructInProgress = false;
        bMoveToStruct = false;
    }

    void Update()
    {
        if (bStructOrderFollow)
        {
            if (!bStructInProgress && !bMoveToStruct)
            {
                if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue.Count > 0)
                {
                    MyStruct();
                }
            }
        }

        if (bStructInProgress)
        {
            Calculate.RotToDest(gameObject, DestStructure.transform.position);
        }
        if (DestStructure != null)
        {
            if (bStructInProgress || bMoveToStruct)  // 건설중 다른곳으로 이동
            {
                if (!DestStructure.GetComponent<Collider2D>().OverlapPoint(GetComponent<EngineerFSM>().m_vecDest))
                {
                    bStructOrderFollow = false;
                    bStructInProgress = false;
                    bMoveToStruct = false;

                    if (!DestStructure.GetComponent<Build>().m_bStructStarted)
                        Calculate.ChangeTileOptionInSize(Calculate.GetMyTileIdx(DestStructure.transform.position), DestStructure.GetComponent<BuildStatus>().m_BuildData.vSize, TILE_OPTION.BUILD_QUEUED);

                    DestStructure.GetComponent<Build>().bStructInProgress = false;
                    GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue.Insert(0, DestStructure);
                    DestStructure = null;
                    GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
                    //GetComponent<CircleCollider2D>().isTrigger = false;
                }
            }
        }

        if (!bStructOrderFollow) // 이동명령으로 건물짓도록이동시킬때
        {
            for (int i = 0; i < GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue.Count; ++i)
            {
                List<int> iDestSizeTile = Calculate.GetSizeTileInHere(Calculate.GetMyTileIdx(GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue[i].transform.position), GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue[i].GetComponent<BuildStatus>().m_BuildData.vSize);

                for (int j = 0; j < iDestSizeTile.Count; ++j)
                {
                    if (iDestSizeTile[j] == Calculate.GetMyTileIdx(GetComponent<EngineerFSM>().m_vecDest))
                    {
                        MyStruct(i);
                    }
                }
            }
        }
    }

    public void StructComplete()
    {
        bStructOrderFollow = true;
        bMoveToStruct = false;
        bStructInProgress = false;
        StateToIdle();
        DestStructure = null;

        if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue.Count > 0)
        {
            MyStruct();
        }
    }

    void MyStruct(int iIndex = 0)
    {

        DestStructure = GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue[iIndex];


        Calculate.ChangeTileOptionInSize(Calculate.GetMyTileIdx(DestStructure.transform.position), DestStructure.GetComponent<BuildStatus>().m_BuildData.vSize, TILE_OPTION.IN_STRUCT);
        GetComponent<EngineerFSM>().m_vecDest = new Vector3(DestStructure.transform.localPosition.x, DestStructure.transform.localPosition.y);

        if (!Calculate.AStarStart(gameObject, GetComponent<EngineerFSM>().m_vecDest, ref (GetComponent<EngineerFSM>().m_BestList), GetComponent<EngineerFSM>().m_iPrevTIleIdx, GetComponent<EngineerFSM>().m_iPrevTileOption))
        {
            GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue.RemoveAt(iIndex);
            DestStructure.GetComponent<Build>().Builder = gameObject;
            bMoveToStruct = true;
            

            GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
        }
        else // Cant find way
        {
            for (int j = 0; j < GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue.Count; ++j)
            {
                if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue[j] == DestStructure)
                {
                    GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue.Add(GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue[j]);
                    GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().StructQueue.RemoveAt(j);
                    break;
                }
            }
            Calculate.ChangeTileOptionInSize(Calculate.GetMyTileIdx(DestStructure.transform.position), DestStructure.GetComponent<BuildStatus>().m_BuildData.vSize, TILE_OPTION.BUILD_QUEUED);

            bStructOrderFollow = false;
            bMoveToStruct = false;
            //Destroy(DestStructure.gameObject);
            DestStructure = null;
            GetComponent<EngineerFSM>().m_vecDest = transform.position;
        }
    }



    public void StateToIdle()
    {
        GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
    }

    void OnDestinationInvalid()
    {
        //if (!GetComponent<PolyNavAgent>().hasPath && !bMoveToStruct)
        //{
        //    GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
        //}
    }

}